tool
extends EditorScript


# 导入文件路径
const mod_path = "res://destiny_mod.dym_area.zip"
# 解密密钥
const secret = "12345678"

const release = true

const file_prefix = "res://map/assets/"

const split = 100  # 同时进行的解压数量

var cnt = 0 # 当前解压文件数量
var max_cnt = 0 # 最大解压文件数量

var data = null

func _run():
	print("load mode.....")
	var files = load_mode(mod_path, secret, release)
	print("load mode finsh.....")
	max_cnt = files.size()
	cnt = 0
	
	var directory = Directory.new()
	
	for i in files:
		if i["filename"] == "areaData.json":
			var content:String = i["content"].get_string_from_utf8()
			var area_info = str2var(content).get("areaInfo", {})
			
			print("\narea_info==>:\n", area_info)
		
		var filename = file_prefix + i["filename"]
		
		if i["filename"].ends_with("/"):
			directory.make_dir_recursive(filename)
	
	print("create dir finsh.....")
		
	var files_array = split_array_into_n(files, 100)
	for file_temp in files_array:

		if file_temp.size() != 0:
			uncompress_files(file_temp)
	
	print("load finsh.....")

# 将一个数组分隔数为n个数组组
func split_array_into_n(arr, n):  
	var result = []  
	var size = arr.size()  
	var chunk_size = size / n  # 整除，得到每个子数组应有的元素数量  
	var extra = size % n     # 计算余数，即额外的元素数量  
	  
	for i in range(n):  
		var start = i * chunk_size  
		if i!=0: start += extra 
		var end = start + chunk_size  
		if i==0:end += extra 
		  
		result.append(arr.slice(start, end-1))  
	  
	return result 


# 解压文件数组
func uncompress_files(files):
	var file = File.new()
	var a = 0
	for i in files:		
		if i["filename"].ends_with("/"):
			pass
		else:
			var file_name =  file_prefix + i["filename"]
			if i["filename"] == "areaData.json":
				file_name =  "res://" + i["filename"]
			
			save_file_bytes(i["content"], file_name)
		
		cnt += 1



static func save_file_bytes(pool_byte, path):
	var file = File.new()
	file.open(path,File.WRITE)
	file.store_buffer(pool_byte)
	file.close()


static func load_mode(mode_file, secret="",release = true):
	print("read ", mode_file)
	var read_file = File.new()
	var res = 0
	if release:
		res = read_file.open_encrypted_with_pass(mode_file, File.READ, secret)
	else:
		res = read_file.open(mode_file,File.READ)
		
	if res != OK:
		printerr(str("mod读取失败, 请确认是否导入正确的mod文件:", res))
		return []
		

	var total_size = read_file.get_32()
	
	# 解压缩
	var compress_buffer = read_file.get_buffer(read_file.get_len() - read_file.get_position())
	# 如果是false 那么需要单独解密
	if !release:
		compress_buffer = AESUtil.decrypt(compress_buffer,secret)
	
	# 原始数据
	var buffer = compress_buffer.decompress(total_size, File.COMPRESSION_GZIP)
	var buffer_len = buffer.size()
		
	var new_file = File.new()
	
	# 写入临时文件
	var temp_file = "user://xajbqwu.t"
	new_file.open(temp_file, File.WRITE_READ)
	new_file.store_buffer(buffer)
	
	new_file.seek(0)
	
	var files = []
	
	# 解析
	while new_file.get_position() < new_file.get_len():
		var file_name_length = new_file.get_32()
		var content_length = new_file.get_32()
		var is_encry = new_file.get_8()
		
		var file_name = new_file.get_buffer(file_name_length).get_string_from_utf8()
		
		var content = new_file.get_buffer(content_length)
		
		files.append({
			"filename": file_name,
			"content": content,
			"is_encry": is_encry == 1
		})
	
	new_file.close()
	# 删除临时文件
	Directory.new().remove(temp_file)
	
	return files
	

